package com.peycard.vista;

import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.validator.Max;
import org.hibernate.validator.Min;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Size;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.peycard.common.CompanyDTO;
import com.peycard.service.BaseService;
import com.peycard.util.Constantes;
import com.peycard.util.Utils;

@Controller(Constantes.BEAN_COMPANY)
@Scope(Constantes.SCOPE_REQUEST)
public class ComerciosControlador extends BaseController implements
		Serializable {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ComerciosControlador.class);
	private long idCompany;
	@NotEmpty
	private String tradename;
	@NotEmpty
	private String socialReason;
	@NotEmpty
	private String corporateSpin;
	@NotEmpty
	private String rfc;
	@NotEmpty
	private String infonavit;
	@NotEmpty
	private String legalRepresentative;
	@NotEmpty
	private String stps;
	@NotEmpty
	private String stpsRepresentative;
	@NotEmpty
	private String country;
	@NotEmpty
	private String state;
	@NotEmpty
	private String street;
	@NotEmpty
	private String numberInEx;
	@NotEmpty
	private String suburb;
	@NotEmpty
	private String zipCode;
	@NotEmpty
	private String telephone1;
	private String telephone2;
	@NotEmpty
	private String fax;
	@NotEmpty
	private String email;
	private String web;
	@NotNull
	@Size(min = 0, max = 1, message="Debe escoger un estado.")
	private int isActive;
	private String username;
	private String currentToken;
	private List<CompanyDTO> listItems;
	private CompanyDTO currentItem;

	@NotEmpty
	private String newTradename;
	@NotEmpty
	private String newSocialReason;
	@NotEmpty
	private String newCorporateSpin;
	@NotEmpty
	private String newRfc;
	@NotEmpty
	private String newInfonavit;
	@NotEmpty
	private String newLegalRepresentative;
	@NotEmpty
	private String newStps;
	@NotEmpty
	private String newStpsRepresentative;
	@NotEmpty
	private String newCountry;
	@NotEmpty
	private String newState;
	@NotEmpty
	private String newStreet;
	@NotEmpty
	private String newNumberInEx;
	@NotEmpty
	private String newSuburb;
	@NotEmpty
	private String newZipCode;
	@NotEmpty
	private String newTelephone1;
	private String newTelephone2;
	@NotEmpty
	private String newFax;
	@NotEmpty
	private String newEmail;
	private String newWeb;
	@NotNull
	@Size(min = 0, max = 1, message="Debe escoger un estado.")
	private int newIsActive;
	private String newUsername;

	private String viewTradename;
	private String viewSocialReason;
	private String viewCorporateSpin;
	private String viewRfc;
	private String viewInfonavit;
	private String viewLegalRepresentative;
	private String viewStps;
	private String viewStpsRepresentative;
	private String viewCountry;
	private String viewState;
	private String viewStreet;
	private String viewNumberInEx;
	private String viewSuburb;
	private String viewZipCode;
	private String viewTelephone1;
	private String viewTelephone2;
	private String viewFax;
	private String viewEmail;
	private String viewWeb;
	private int viewIsActive;
	private String viewUsername;
	private CompanyDTO seeItem;
	private String deleteId;
	private CompanyDTO deleteItem;

	public ComerciosControlador() {
		init();
		currentToken = super.getTokenInContext();
		username = super.userLogged();
	}

	public void init() {
		try {
			BaseService service = new BaseService();
			service.setToken(currentToken);
			service.listISOMessage(Constantes.ISO003_COMPANY_LIST,
					Constantes.COMPANY_USERNAME, username,
					Constantes.COMPANY_LIST_KEY);
			List<String> companyList = service.getList();
			LOGGER.info("[**" + companyList + "**]");
			if (companyList != null) {
				listItems = new ArrayList<CompanyDTO>();

				for (int i = 0; i < companyList.size(); i++) {
					String currentCompany = companyList.get(i);
					List<String> companyData = Utils
							.convertList(currentCompany);
					CompanyDTO dto = new CompanyDTO();
					long id = Long.valueOf(companyData.get(0));
					dto.setIdCompany(id);
					dto.setTradename(companyData.get(1));
					dto.setSocialReason(companyData.get(2));
					dto.setCorporateSpin(companyData.get(3));
					dto.setRfc(companyData.get(4));
					dto.setInfonavit(companyData.get(5));
					dto.setLegalRepresentative(companyData.get(6));
					dto.setStps(companyData.get(7));
					dto.setStpsRepresentative(companyData.get(8));
					dto.setCountry(companyData.get(9));
					dto.setState(companyData.get(10));
					dto.setStreet(companyData.get(11));
					dto.setNumberInEx(companyData.get(12));
					dto.setSuburb(companyData.get(13));
					dto.setZipCode(companyData.get(14));
					dto.setTelephone1(companyData.get(15));
					dto.setTelephone2(companyData.get(16));
					dto.setFax(companyData.get(17));
					dto.setEmail(companyData.get(18));
					dto.setWeb(companyData.get(19));
					dto.setIsActive(Short.valueOf(companyData.get(20)));
					listItems.add(dto);
				}
				super.setTokenInContext(service.getToken());
			} else {
				super.addErrorMessage(service.getMessage());
			}
		} catch (IOException e) {
			super.addErrorMessage("No fue posible conectarse con el servidor.");
		} catch (ParseException e) {
			super.addWarningMessage("Ocurrió un error al parsear la petición.");
		}
	}

	public String createCompany() {
		newUsername = "jamaro";
		List<Object> companyData = new ArrayList<Object>();
		companyData.add(newTradename);
		companyData.add(newSocialReason);
		companyData.add(newCorporateSpin);
		companyData.add(newRfc);
		companyData.add(newInfonavit);
		companyData.add(newLegalRepresentative);
		companyData.add(newStps);
		companyData.add(newStpsRepresentative);
		companyData.add(newCountry);
		companyData.add(newState);
		companyData.add(newStreet);
		companyData.add(newNumberInEx);
		companyData.add(newSuburb);
		companyData.add(newZipCode);
		companyData.add(newTelephone1);
		companyData.add(newTelephone2);
		companyData.add(newFax);
		companyData.add(newEmail);
		companyData.add(newWeb);
		companyData.add(1);
		companyData.add(newUsername);

		String string = Utils.buildStringData(companyData);
		createUpdateCompanyRequest(Constantes.ISO003_NEW_COMPANY, string);
		return "";
	}

	public String updateCompany() {
		username = "jamaro";
		List<Object> companyData = new ArrayList<Object>();
		companyData.add(idCompany);
		companyData.add(tradename);
		companyData.add(socialReason);
		companyData.add(corporateSpin);
		companyData.add(rfc);
		companyData.add(infonavit);
		companyData.add(legalRepresentative);
		companyData.add(stps);
		companyData.add(stpsRepresentative);
		companyData.add(country);
		companyData.add(state);
		companyData.add(street);
		companyData.add(numberInEx);
		companyData.add(suburb);
		companyData.add(zipCode);
		companyData.add(telephone1);
		companyData.add(telephone2);
		companyData.add(fax);
		companyData.add(email);
		companyData.add(web);
		companyData.add(isActive);
		companyData.add(username);

		String string = Utils.buildStringData(companyData);
		createUpdateCompanyRequest(Constantes.ISO003_UPDATE_COMPANY, string);
		return "";
	}

	public String deleteCompany() {
		username = "jamaro";
		List<Object> deleteCompanyData = new ArrayList<Object>();
		deleteCompanyData.add(idCompany);
		deleteCompanyData.add(username);
		String deleteString = Utils.buildStringData(deleteCompanyData);
		try {
			BaseService service = new BaseService();
			service.setToken(currentToken);
			boolean response = service.isoMessageRequest(
					Constantes.ISO003_DELETE_COMPANY,
					Constantes.DELETE_COMPANY_METADATA, deleteString);
			if (response) {
				super.setTokenInContext(service.getToken());
			} else {
				super.addErrorMessage(service.getMessage());
			}
		} catch (IOException e) {
			super.addErrorMessage("No fue posible conectarse con el servidor.");
		} catch (ParseException e) {
			super.addWarningMessage("Ocurrió un error al parsear la petición.");
		}
		return "";
	}

	public void createUpdateCompanyRequest(String proccessingCode,
			String dataStructure) {
		String metadata = null;
		try {
			if (proccessingCode.equals(Constantes.ISO003_NEW_COMPANY)) {
				metadata = Constantes.CREATE_COMPANY_METADATA;

			} else if (proccessingCode.equals(Constantes.ISO003_UPDATE_COMPANY)) {
				metadata = Constantes.UPDATE_COMPANY_METADATA;
			}
			BaseService service = new BaseService();
			service.setToken(currentToken);
			boolean response = service.isoMessageRequest(proccessingCode,
					metadata, dataStructure);
			if (response) {
				super.setTokenInContext(service.getToken());
			} else {
				super.addErrorMessage(service.getMessage());
			}
		} catch (IOException e) {
			super.addErrorMessage("No fue posible conectarse con el servidor.");
		} catch (ParseException e) {
			super.addWarningMessage("Ocurrió un error al parsear la petición.");
		}
	}

	private void setValues(CompanyDTO dto) {
		idCompany = dto.getIdCompany();
		tradename = dto.getTradename();
		socialReason = dto.getSocialReason();
		corporateSpin = dto.getCorporateSpin();
		rfc = dto.getRfc();
		infonavit = dto.getInfonavit();
		legalRepresentative = dto.getLegalRepresentative();
		stps = dto.getStps();
		stpsRepresentative = dto.getStpsRepresentative();
		country = dto.getCountry();
		state = dto.getState();
		street = dto.getStreet();
		numberInEx = dto.getNumberInEx();
		suburb = dto.getSuburb();
		zipCode = dto.getZipCode();
		telephone1 = dto.getTelephone1();
		telephone2 = dto.getTelephone2();
		fax = dto.getFax();
		email = dto.getEmail();
		web = dto.getWeb();
		isActive = dto.getIsActive();
	}

	private void info(CompanyDTO dto) {
		viewIsActive = dto.getIsActive();

		if (dto.getTradename() != null) {
			viewTradename = dto.getTradename();
		}
		if (dto.getSocialReason() != null) {
			viewSocialReason = dto.getSocialReason();
		}
		if (dto.getCorporateSpin() != null) {
			viewCorporateSpin = dto.getCorporateSpin();
		}
		if (dto.getRfc() != null) {
			viewRfc = dto.getRfc();
		}
		if (dto.getInfonavit() != null) {
			viewInfonavit = dto.getInfonavit();
		}
		if (dto.getLegalRepresentative() != null) {
			viewLegalRepresentative = dto.getLegalRepresentative();
		}
		if (dto.getStps() != null) {
			viewStps = dto.getStps();
		}
		if (dto.getStpsRepresentative() != null) {
			viewStpsRepresentative = dto.getStpsRepresentative();
		}
		if (dto.getCountry() != null) {
			viewCountry = dto.getCountry();
		}
		if (dto.getState() != null) {
			viewState = dto.getState();
		}
		if (dto.getStreet() != null) {
			viewStreet = dto.getStreet();
		}
		if (dto.getNumberInEx() != null) {
			viewNumberInEx = dto.getNumberInEx();
		}
		if (dto.getSuburb() != null) {
			viewSuburb = dto.getSuburb();
		}
		if (dto.getZipCode() != null) {
			viewZipCode = dto.getZipCode();
		}
		if (dto.getTelephone1() != null) {
			viewTelephone1 = dto.getTelephone1();
		}
		if (dto.getTelephone2() != null) {
			viewTelephone2 = dto.getTelephone2();
		}
		if (dto.getFax() != null && dto.getFax().equals("null")) {
			viewFax = dto.getFax();
		}
		if (dto.getEmail() != null) {
			viewEmail = dto.getEmail();
		}
		if (dto.getWeb() != null) {
			viewWeb = dto.getWeb();
		}
	}

	public String linkCompany() {
		return "goToLinkerPage";
	}

	public long getIdCompany() {
		return idCompany;
	}

	public void setIdCompany(long idCompany) {
		this.idCompany = idCompany;
	}

	public String getTradename() {
		return tradename;
	}

	public void setTradename(String tradename) {
		this.tradename = tradename;
	}

	public String getSocialReason() {
		return socialReason;
	}

	public void setSocialReason(String socialReason) {
		this.socialReason = socialReason;
	}

	public String getRfc() {
		return rfc;
	}

	public void setRfc(String rfc) {
		this.rfc = rfc;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getNumberInEx() {
		return numberInEx;
	}

	public void setNumberInEx(String numberInEx) {
		this.numberInEx = numberInEx;
	}

	public String getSuburb() {
		return suburb;
	}

	public void setSuburb(String suburb) {
		this.suburb = suburb;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getTelephone1() {
		return telephone1;
	}

	public void setTelephone1(String telephone1) {
		this.telephone1 = telephone1;
	}

	public String getTelephone2() {
		return telephone2;
	}

	public void setTelephone2(String telephone2) {
		this.telephone2 = telephone2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWeb() {
		return web;
	}

	public void setWeb(String web) {
		this.web = web;
	}

	public List<CompanyDTO> getListItems() {
		return listItems;
	}

	public void setListItems(List<CompanyDTO> listItems) {
		this.listItems = listItems;
	}

	public CompanyDTO getCurrentItem() {
		return currentItem;
	}

	public void setCurrentItem(CompanyDTO currentItem) {
		setValues(currentItem);
		this.currentItem = currentItem;
	}

	public String getCorporateSpin() {
		return corporateSpin;
	}

	public void setCorporateSpin(String corporateSpin) {
		this.corporateSpin = corporateSpin;
	}

	public String getInfonavit() {
		return infonavit;
	}

	public void setInfonavit(String infonavit) {
		this.infonavit = infonavit;
	}

	public String getLegalRepresentative() {
		return legalRepresentative;
	}

	public void setLegalRepresentative(String legalRepresentative) {
		this.legalRepresentative = legalRepresentative;
	}

	public String getStps() {
		return stps;
	}

	public void setStps(String stps) {
		this.stps = stps;
	}

	public String getStpsRepresentative() {
		return stpsRepresentative;
	}

	public void setStpsRepresentative(String stpsRepresentative) {
		this.stpsRepresentative = stpsRepresentative;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public int getIsActive() {
		return isActive;
	}

	public void setIsActive(int isActive) {
		this.isActive = isActive;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getCurrentToken() {
		return currentToken;
	}

	public void setCurrentToken(String currentToken) {
		this.currentToken = currentToken;
	}

	public String getNewTradename() {
		return newTradename;
	}

	public void setNewTradename(String newTradename) {
		this.newTradename = newTradename;
	}

	public String getNewSocialReason() {
		return newSocialReason;
	}

	public void setNewSocialReason(String newSocialReason) {
		this.newSocialReason = newSocialReason;
	}

	public String getNewCorporateSpin() {
		return newCorporateSpin;
	}

	public void setNewCorporateSpin(String newCorporateSpin) {
		this.newCorporateSpin = newCorporateSpin;
	}

	public String getNewRfc() {
		return newRfc;
	}

	public void setNewRfc(String newRfc) {
		this.newRfc = newRfc;
	}

	public String getNewInfonavit() {
		return newInfonavit;
	}

	public void setNewInfonavit(String newInfonavit) {
		this.newInfonavit = newInfonavit;
	}

	public String getNewLegalRepresentative() {
		return newLegalRepresentative;
	}

	public void setNewLegalRepresentative(String newLegalRepresentative) {
		this.newLegalRepresentative = newLegalRepresentative;
	}

	public String getNewStps() {
		return newStps;
	}

	public void setNewStps(String newStps) {
		this.newStps = newStps;
	}

	public String getNewStpsRepresentative() {
		return newStpsRepresentative;
	}

	public void setNewStpsRepresentative(String newStpsRepresentative) {
		this.newStpsRepresentative = newStpsRepresentative;
	}

	public String getNewCountry() {
		return newCountry;
	}

	public void setNewCountry(String newCountry) {
		this.newCountry = newCountry;
	}

	public String getNewState() {
		return newState;
	}

	public void setNewState(String newState) {
		this.newState = newState;
	}

	public String getNewStreet() {
		return newStreet;
	}

	public void setNewStreet(String newStreet) {
		this.newStreet = newStreet;
	}

	public void setNewNumberInEx(String newNumberInEx) {
		this.newNumberInEx = newNumberInEx;
	}

	public String getNewSuburb() {
		return newSuburb;
	}

	public void setNewSuburb(String newSuburb) {
		this.newSuburb = newSuburb;
	}

	public String getNewZipCode() {
		return newZipCode;
	}

	public void setNewZipCode(String newZipCode) {
		this.newZipCode = newZipCode;
	}

	public String getNewTelephone1() {
		return newTelephone1;
	}

	public void setNewTelephone1(String newTelephone1) {
		this.newTelephone1 = newTelephone1;
	}

	public String getNewTelephone2() {
		return newTelephone2;
	}

	public void setNewTelephone2(String newTelephone2) {
		this.newTelephone2 = newTelephone2;
	}

	public String getNewFax() {
		return newFax;
	}

	public void setNewFax(String newFax) {
		this.newFax = newFax;
	}

	public String getNewEmail() {
		return newEmail;
	}

	public void setNewEmail(String newEmail) {
		this.newEmail = newEmail;
	}

	public String getNewWeb() {
		return newWeb;
	}

	public void setNewWeb(String newWeb) {
		this.newWeb = newWeb;
	}

	public int getNewIsActive() {
		return newIsActive;
	}

	public void setNewIsActive(int newIsActive) {
		this.newIsActive = newIsActive;
	}

	public String getNewUsername() {
		return newUsername;
	}

	public void setNewUsername(String newUsername) {
		this.newUsername = newUsername;
	}

	public String getNewNumberInEx() {
		return newNumberInEx;
	}

	public String getViewTradename() {
		return viewTradename;
	}

	public void setViewTradename(String viewTradename) {
		this.viewTradename = viewTradename;
	}

	public String getViewSocialReason() {
		return viewSocialReason;
	}

	public void setViewSocialReason(String viewSocialReason) {
		this.viewSocialReason = viewSocialReason;
	}

	public String getViewCorporateSpin() {
		return viewCorporateSpin;
	}

	public void setViewCorporateSpin(String viewCorporateSpin) {
		this.viewCorporateSpin = viewCorporateSpin;
	}

	public String getViewRfc() {
		return viewRfc;
	}

	public void setViewRfc(String viewRfc) {
		this.viewRfc = viewRfc;
	}

	public String getViewInfonavit() {
		return viewInfonavit;
	}

	public void setViewInfonavit(String viewInfonavit) {
		this.viewInfonavit = viewInfonavit;
	}

	public String getViewLegalRepresentative() {
		return viewLegalRepresentative;
	}

	public void setViewLegalRepresentative(String viewLegalRepresentative) {
		this.viewLegalRepresentative = viewLegalRepresentative;
	}

	public String getViewStps() {
		return viewStps;
	}

	public void setViewStps(String viewStps) {
		this.viewStps = viewStps;
	}

	public String getViewStpsRepresentative() {
		return viewStpsRepresentative;
	}

	public void setViewStpsRepresentative(String viewStpsRepresentative) {
		this.viewStpsRepresentative = viewStpsRepresentative;
	}

	public String getViewCountry() {
		return viewCountry;
	}

	public void setViewCountry(String viewCountry) {
		this.viewCountry = viewCountry;
	}

	public String getViewState() {
		return viewState;
	}

	public void setViewState(String viewState) {
		this.viewState = viewState;
	}

	public String getViewStreet() {
		return viewStreet;
	}

	public void setViewStreet(String viewStreet) {
		this.viewStreet = viewStreet;
	}

	public String getViewNumberInEx() {
		return viewNumberInEx;
	}

	public void setViewNumberInEx(String viewNumberInEx) {
		this.viewNumberInEx = viewNumberInEx;
	}

	public String getViewSuburb() {
		return viewSuburb;
	}

	public void setViewSuburb(String viewSuburb) {
		this.viewSuburb = viewSuburb;
	}

	public String getViewZipCode() {
		return viewZipCode;
	}

	public void setViewZipCode(String viewZipCode) {
		this.viewZipCode = viewZipCode;
	}

	public String getViewTelephone1() {
		return viewTelephone1;
	}

	public void setViewTelephone1(String viewTelephone1) {
		this.viewTelephone1 = viewTelephone1;
	}

	public String getViewTelephone2() {
		return viewTelephone2;
	}

	public void setViewTelephone2(String viewTelephone2) {
		this.viewTelephone2 = viewTelephone2;
	}

	public String getViewFax() {
		return viewFax;
	}

	public void setViewFax(String viewFax) {
		this.viewFax = viewFax;
	}

	public String getViewEmail() {
		return viewEmail;
	}

	public void setViewEmail(String viewEmail) {
		this.viewEmail = viewEmail;
	}

	public String getViewWeb() {
		return viewWeb;
	}

	public void setViewWeb(String viewWeb) {
		this.viewWeb = viewWeb;
	}

	public int getViewIsActive() {
		return viewIsActive;
	}

	public void setViewIsActive(int viewIsActive) {
		this.viewIsActive = viewIsActive;
	}

	public String getViewUsername() {
		return viewUsername;
	}

	public void setViewUsername(String viewUsername) {
		this.viewUsername = viewUsername;
	}

	public CompanyDTO getSeeItem() {
		return seeItem;
	}

	public void setSeeItem(CompanyDTO seeItem) {
		info(seeItem);
		this.seeItem = seeItem;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public CompanyDTO getDeleteItem() {
		return deleteItem;
	}

	public void setDeleteItem(CompanyDTO deleteItem) {
		this.deleteItem = deleteItem;
	}

}
